﻿namespace AgileZen.Models
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;
    using System;
    
    [DataContract(Name = "comment")]
    public class Comment : AgileZenModel
    {
        private int _id;
        private string _text;
        private Person _author;
       
        [DataMember(Name = "id", Order = 3, EmitDefaultValue = false)]
        public int Id
        {
            get { return _id; }
            set { _id = value; NotifyPropertyChanged("Id"); }
        }

        public bool ShouldSerializeId()
        {
            return PropertiesChanged.Contains("Id");
        }

        [DataMember(Name = "text", Order = 9, EmitDefaultValue = false)]
        public string Text 
        {
            get { return _text; }
            set { _text = value; NotifyPropertyChanged("Text"); } 
        }

        public bool ShouldSerializeText()
        {
            return PropertiesChanged.Contains("Text");
        }

        [DataMember(Name = "createTime", Order = 0)]
        private string CreatedOnUTCString { get; set; }

        public DateTime? CreatedOn
        {
            get
            {
                bool parsed = false;
                DateTime datetime = DateTime.MinValue;
                if (!String.IsNullOrEmpty(CreatedOnUTCString))
                {
                    parsed = DateTime.TryParse(CreatedOnUTCString, out datetime);
                }

                return (parsed) ? datetime : new Nullable<DateTime>();
            }
        }

        [DataMember(Name = "author", Order = 2, EmitDefaultValue = false)]
        public Person Author
        {
            get { return _author; }
            set { _author = value; NotifyPropertyChanged("Author"); }
        }

        public bool ShouldSerializeAuthor()
        {
            return PropertiesChanged.Contains("Author");
        }
    }
}
